function UserInvitation(from, to){
	this.from = from;
	this.to = to;
}

InvitationManager = {
	
	container : null,
		
	intervalSet : false,
	
	invitationsOpened : false,
	
	interval : 20000,
	
	invitations : [],
	
	invitationsBox : [],
	
	initialize : function(container){
		InvitationManager.container = container;
		InvitationManager.container.notify({stack:'above'});
		if(isLogged == "true")
		{
			InvitationManager.refreshInvitations();
			
			//check for new invitations
			if (!InvitationManager.intervalSet) {
				// repeat each 30 seconds
				window.setInterval(function() {
					InvitationManager.refreshInvitations();
				}, InvitationManager.interval);
				
				InvitationManager.intervalSet = true;
			}
			
			$("#invitationsButton").click(InvitationManager.invitationButtonClick);
		}
	},
	
	refreshInvitations : function(){
		Api.me.getInvitations(function(data) {
			
			InvitationManager.invitations = [];
			if(data.invitations.length > 0)
			{
				$.each(data.invitations, function(key, val) {
					InvitationManager.invitations.push(val);
				});
				
				var count = InvitationManager.invitations.length;
				if(count > 0) $("#invitationsCount").text('{' + count + '}');
				return;
			}
			else
			{
				$("#invitationsCount").text('');
			}
		});

	},
	
	create : function(template, vars, options){
		return InvitationManager.container.notify("create", template, vars, options);
	},
	
	addInvitation : function(from){

		var instance = InvitationManager.create("defaultInvitation", {title: I18N.msg.invitationTitle, text: from }, { custom : true, expires : false });
		InvitationManager.invitationsBox.push(instance);
		
		instance.widget().on("click", "input.confirm", function(){
			Api.me.setInvitation(from, 'true', function(data) {
				InvitationManager.refreshInvitations();
			});
			instance.close();
		});
		
		instance.widget().on("click", "input.decline", function(){
			Api.me.setInvitation(from, 'false', function(data) {
				InvitationManager.refreshInvitations();
			});
			instance.close();
		});
		
		$("input.confirm").val(I18N.msg.invitationAccept);
		$("input.decline").val(I18N.msg.invitationDecline);
	},
	
	invitationButtonClick : function(event) {
		
		if(InvitationManager.invitationsOpened)
		{
			for(var i = 0; i < InvitationManager.invitationsBox.length; i++)
			{
				InvitationManager.invitationsBox[i].close();
			}
			InvitationManager.invitationsBox = [];
			
			InvitationManager.invitationsOpened = false;
		}
		else
		{
			$.each(InvitationManager.invitations, function(key, val) {
				
					InvitationManager.addInvitation(val.from);
			});
			InvitationManager.invitationsOpened = true;
		}
	}
		
		
		
};